package com.jiangyg.mall.member.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jiangyg.mall.core.enums.Enable;
import com.jiangyg.mall.core.support.restful.Pagination;
import com.jiangyg.mall.core.support.snowflake.SnowflakeBuilder;
import com.jiangyg.mall.core.utils.bean.BeanUtils;
import com.jiangyg.mall.db.mybatis.BaseServiceImpl;
import com.jiangyg.mall.member.constant.ColumnConstant;
import com.jiangyg.mall.member.dao.MemberReceiveAddressDao;
import com.jiangyg.mall.member.dto.MemberReceiveAddressDTO;
import com.jiangyg.mall.member.entity.MemberReceiveAddress;
import com.jiangyg.mall.member.vo.MemberReceiveAddressVO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MemberReceiveAddressServiceImpl extends BaseServiceImpl<MemberReceiveAddressDao, MemberReceiveAddress> implements MemberReceiveAddressService {

    @Autowired
    public MemberReceiveAddressServiceImpl(MemberReceiveAddressDao memberReceiveAddressDao) {
        super(memberReceiveAddressDao);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public Pagination<MemberReceiveAddressVO> selectPagination(Pagination<MemberReceiveAddressVO> pagination) throws Exception {
        return this.executeSelectPagination(pagination, MemberReceiveAddressVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MemberReceiveAddressVO saveOrUpdate(MemberReceiveAddressDTO dto, Long memberId) throws Exception {
        // 因为同一会员只能有一个默认地址，所以新增或者修改的地址为默认的话，需要重置其他地址的状态
        if (Enable.decide(dto.getIsDefault()) == Enable.Y) {
            final UpdateWrapper<MemberReceiveAddress> wrapper = new UpdateWrapper<>();
            wrapper.eq(ColumnConstant.MEMBER_ID, memberId);
            final MemberReceiveAddress receiveAddress = new MemberReceiveAddress();
            receiveAddress.setIsDefault(Enable.N.icode);
            this.baseMapper.update(receiveAddress, wrapper);
        }
        // 传入地址ID的话，尝试修改
        if (dto.getId() != null) {
            final MemberReceiveAddress receiveAddress = this.baseMapper.selectById(dto.getId());
            if (receiveAddress != null) {
                BeanUtils.copyProperties(receiveAddress, dto);
                final UpdateWrapper<MemberReceiveAddress> wrapper = new UpdateWrapper<>();
                wrapper.eq(ColumnConstant.ID, dto.getId());
                wrapper.eq(ColumnConstant.MEMBER_ID, memberId);
                this.baseMapper.update(receiveAddress, wrapper);
                return BeanUtils.transform(receiveAddress, MemberReceiveAddressVO.class);
            }
        }
        // 尝试新增
        final MemberReceiveAddress receiveAddress = new MemberReceiveAddress();
        BeanUtils.copyProperties(receiveAddress, dto);
        receiveAddress.setMemberId(memberId);
        receiveAddress.setId(SnowflakeBuilder.nextId());
        this.baseMapper.insert(receiveAddress);
        return BeanUtils.transform(receiveAddress, MemberReceiveAddressVO.class);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public MemberReceiveAddressVO selectById(Long receiveAddressId, Long memberId) throws Exception {
        final QueryWrapper<MemberReceiveAddress> wrapper = new QueryWrapper<>();
        wrapper.eq(ColumnConstant.ID, receiveAddressId);
        wrapper.eq(ColumnConstant.MEMBER_ID, memberId);
        final List<MemberReceiveAddress> dataList = this.baseMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(dataList)) {
            return null;
        }
        return BeanUtils.transform(dataList.get(0), MemberReceiveAddressVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(List<Long> receiveAddressIds, Long memberId) {
        final UpdateWrapper<MemberReceiveAddress> wrapper = new UpdateWrapper<>();
        wrapper.in(ColumnConstant.ID, receiveAddressIds);
        wrapper.eq(ColumnConstant.MEMBER_ID, memberId);
        return this.baseMapper.delete(wrapper) > 0;
    }

    @Override
    public List<MemberReceiveAddressVO> selectListByMemberId(Long memberId) throws Exception {
        final QueryWrapper<MemberReceiveAddress> wrapper = new QueryWrapper<>();
        wrapper.eq(ColumnConstant.MEMBER_ID, memberId);
        return BeanUtils.transformList(baseMapper.selectList(wrapper), MemberReceiveAddressVO.class);
    }

}
